package com.xiaofeng.server.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.xiaofeng.api.common.Result;
import com.xiaofeng.api.req.GetPracticeSubjectListReq;
import com.xiaofeng.api.req.GetPracticeSubjectReq;
import com.xiaofeng.api.req.GetPracticeSubjectsReq;
import com.xiaofeng.api.vo.*;
import com.xiaofeng.server.entity.dto.PracticeSubjectDTO;
import com.xiaofeng.server.service.PracticeSetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @Author: chc
 * @Date: 2024/04/17/10:42
 * @Description:
 */
@RestController
@Slf4j
@RequestMapping("/practice/set")
public class PracticeSetController {

    @Autowired
    private PracticeSetService practiceSetService;;

    /**
     * 获取专题练习内容接口
     * @return
     */
    @RequestMapping("/getSpecialPracticeContent")
    public Result<List<SpecialPracticeVO>> getSpecialPracticeContent(){
        try {
            List<SpecialPracticeVO> result = practiceSetService.getSpecialPracticeContent();
            if (log.isInfoEnabled()){
                log.info("PracticeSetController.getSpecialPracticeContent.result:{}",result);
            }
            return Result.ok(result);
        } catch (Exception e) {
            log.error("PracticeSetController.getSpecialPracticeContent.error:{}",e.getMessage(),e);
            return Result.fail("获取专题练习内容失败");
        }
    }

    /**
     * 添加套题接口
     */
    @RequestMapping("/addPractice")
    public Result<PracticeSetVo> addPractice(@RequestBody GetPracticeSubjectListReq req){
        if (log.isInfoEnabled()){
            log.info("获取练习题入参{}", JSON.toJSONString(req));
        }
        try {
            //参数校验
            Preconditions.checkArgument(!Objects.isNull(req),"参数不能为空!");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(req.getAssembleIds()),"标签ids不能为空");
            PracticeSubjectDTO dto = new PracticeSubjectDTO();
            dto.setAssembleIds(req.getAssembleIds());
            PracticeSetVo practiceSetVO = practiceSetService.addPractice(dto);
            if (log.isInfoEnabled()){
                log.info("获取练习题目列表出参:{}",JSON.toJSONString(practiceSetVO));
            }
            return Result.ok(practiceSetVO);
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e){
            log.error("获取练习题目列表异常！错误原因{}", e.getMessage(), e);
            return Result.fail("获取练习题目列表异常！");
        }
    }

    /**
     * 获取练习题
     * @param req
     * @return
     */
    @PostMapping(value = "/getSubjects")
    public Result<PracticeSubjectListVO> getSubjects(@RequestBody GetPracticeSubjectsReq req){
        if (log.isInfoEnabled()){
            log.info("获取练习题入参{}",JSON.toJSONString(req));
        }
        try {
            // 参数校验
            Preconditions.checkArgument(!Objects.isNull(req),"参数不能为空!");
            Preconditions.checkArgument(!Objects.isNull(req.getSetId()),"练习id不能为空!");
            PracticeSubjectListVO list = practiceSetService.getSubjects(req);
            if (log.isInfoEnabled()){
                log.info("获取练习题目列表出参:{}",JSON.toJSONString(list));
            }
            return Result.ok(list);
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e){
            log.error("获取练习题目列表异常！错误原因{}",e.getMessage(),e);
            return Result.fail("获取练习题目列表异常！");
        }
    }

    /**
     * 获取题目详情
     * @param req
     * @return
     */
    @PostMapping("/getPracticeSubject")
    public Result<PracticeSubjectVO> getPracticeSubject(@RequestBody GetPracticeSubjectReq req){
        if(log.isInfoEnabled()){
            log.info("获取练习题详情入参{}",JSON.toJSONString(req));
        }
        Preconditions.checkArgument(!Objects.isNull(req),"参数不能为空！");
        Preconditions.checkArgument(!Objects.isNull(req.getSubjectId()),"题目id不能为空！");
        Preconditions.checkArgument(!Objects.isNull(req.getSubjectType()), "题目类型不能为空！");
        PracticeSubjectDTO dto = new PracticeSubjectDTO();
        dto.setSubjectId(req.getSubjectId());
        // dto.setSubjectType(req.getSubjectType());
        return null;
    }

}
